﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anonymous
{
    class Program
    {
        static void Main(string[] args)
        {
            DelegateHistory();
            StartThread();
            Lambda();

            Console.ReadLine();
        }

        delegate void TestDelegate(string s);
        delegate int del(int i);
        delegate TResult Func<TArg0, TResult>(TArg0 arg0);

        static void Method(string s)
        {
            Console.WriteLine(s);
        }

        private static void DelegateHistory()
        {
            TestDelegate testDelA = new TestDelegate(Method);

            //C# 2.0  Anonymous Method
            TestDelegate testDelB = delegate (string s) { Console.WriteLine(s); };

            //C# 3.0  Lambda Expression
            TestDelegate testDelC = (x) => { Console.WriteLine(x); };

            testDelA("This is a delegate!");
            testDelB("This is a anonymous method!");
            testDelC("This is a lambda expression!");
        }

        private static void StartThread()
        {
            //匿名delegate参数不能是ref或者out修饰
            //匿名函数中不能放置不安全代码
            //匿名方法不能放到is关键字左边
            System.Threading.Thread t1 = new System.Threading.Thread(delegate ()
            {
                System.Console.WriteLine("Hello, Dunso");
            }
            );
        }

        //Lambda表达式
        private static void Lambda()
        {
            del myDelegrate = x => x * x;
            Console.WriteLine(myDelegrate(5));
            Func<int, bool> myFunc = x => x == 5;
            Console.WriteLine(myFunc(4));
        }
    }
}
